## Project: Autocracy UPR project ----------------------------------------------
##        03 Mergings             ----------------------------------------------
## Author:
##   - Chun-Young Park (UGA)
##   - Sang-Hoon Park (UofSC)
##
## Updated: Aug 28 2024
## R version 4.4.0 (2024-04-24)
## Platform: aarch64-apple-darwin20
## Running under: macOS Sonoma 14.6.1

if (!require("devtools")) install.packages("devtools")
if (!require("pacman")) install.packages("pacman")
if (!require("rqog")) remotes::install_github("ropengov/rqog")
pacman::p_load(rqog, rio, tidyverse, dendextend, splitstackshape, psych, tidyverse)
if (!require("dendextendRcpp")) devtools::install_github('talgalili/dendextendRcpp')
if (!require("vdemdata")) devtools::install_github("vdeminstitute/vdemdata")
if (!require("ggrepel")) install.packages("ggrepel")
if (!require("patchwork")) install.packages("patchwork")
if (!require("futurevisions")) devtools::install_github("JoeyStanley/futurevisions")

analysis_data_upr <- readRDS("data/data for analysis/analysis_data_upr.RDS")
controls <- load("data/controls/controls.rdata")

### Merge1: Issues variables merging -------------------------------------------
upr_issues <- 
  analysis_data_upr |> 
  rowid_to_column() |> 
  left_join(issue_df |> 
              dplyr::select(1, 8:15), by = c("rowid"))

### Merge2: Geopolitical affinity ----------------------------------------------
upr_issues_affin <- 
  upr_issues |> 
  left_join(
    geopol.affinity |> 
      drop_na(ccode1, ccode2, year), 
    by = c("from_cow" = "ccode1", 
           "to_cow" = "ccode2", 
           "year")) |> 
  dplyr::select(-c(session)) |> 
  mutate(
    idealdistance = abs(ideal_point_all_x - ideal_point_all_y)) |> 
  dplyr::select(-c(ideal_point_all_x, ideal_point_all_y))

### Merge3: Alliance -----------------------------------------------------------
base_sample <- 
  upr_issues_affin |> 
  left_join(
    alliance |> 
      drop_na(ccode1, ccode2, year), 
    by = c("from_cow" = "ccode1", 
           "to_cow" = "ccode2", "year" = "year")) |> 
  mutate(alliance = if_else(is.na(alliance), 0L, alliance)) |> 
  unique()

# Syria and Lebanon
# alliance |> dplyr::filter(ccode2 == 660, ccode1 == 652, year == 2010)

### Merge 4: GDPpc (constant $2015) from WDI --------------------------------------------
base_sample_gdp <- 
  base_sample |> 
  left_join(
    qog_sub |> 
      drop_na(ccodecow, year),
    by = c("to_cow" = "ccodecow", "year")
)

base_sample_gdp |> rename(
  gdppc_sender = wdi_gdpcapcon2015
) -> base_sample_gdp

base_sample_gdp <- 
  base_sample_gdp |> 
  left_join(
    qog_sub |> drop_na(ccodecow, year),
    by = c("from_cow" = "ccodecow", "year")
)

base_sample_gdp |> rename(
  gdppc_target = wdi_gdpcapcon2015
) -> base_sample_gdp

### Base sample: UPR + ISSUES + GEO_AFFINITY + ALLIANCE + GDPPC

### Merge5: Democracy
#### Target (reviewed) democracy -----------------------------------------------
upr_vdem <- 
  base_sample_gdp |>
  drop_na(to_cow, year) |> 
  left_join(
    vdem_sample |> 
      drop_na(COWcode, year),
    by = c("to_cow" = "COWcode", "year" = "year")) |>
  rename(Target_Democracy = v2x_polyarchy,
         Target_polity2 = e_polity2,
         Target_Poly_regime = regime_poly,
         Target_Lexi_regime = regime_lexi,
         Target_Lexi_regime5 = regime_lexi5,
         Target_Lexi_regime4 = regime_lexi4,
         Target_brm_regime = regime_brm,
         Target_Pol4_regime = regime_pol4,
         Target_vdem_regime = regime_vdem,
         Target_region = e_regionpol_6C) |>
  dplyr::select(-c(e_boix_regime, v2xps_party, e_lexical_index, v2x_regime))

#### Sender (reviewing) democracy -----------------------------------------------
upr_vdem <- 
  upr_vdem |> 
  drop_na(from_cow, year) |> 
  left_join(
    vdem_sample |> 
      drop_na(COWcode, year), 
    by = c("from_cow" = "COWcode", "year" = "year")) |>
  rename(Sender_Democracy = v2x_polyarchy,
         Sender_polity2 = e_polity2,
         Sender_Poly_regime = regime_poly,
         Sender_Lexi_regime = regime_lexi,
         Sender_Lexi_regime5 = regime_lexi5,
         Sender_Lexi_regime4 = regime_lexi4,
         Sender_brm_regime = regime_brm,
         Sender_Pol4_regime = regime_pol4,
         Sender_vdem_regime = regime_vdem,
         Sender_region = e_regionpol_6C) |>
  dplyr::select(-c(e_boix_regime, v2xps_party, e_lexical_index, v2x_regime))

#### Joint democracy (Sender - Target); Difference in the level of democracy----
upr_vdem |> 
  group_by(to_cow, from_cow, year) |>
  mutate(
    politysenderminustarget = Sender_Democracy - Target_Democracy,
    regionalism = if_else(Target_region == Sender_region, 1L, 0L)) |>
  ungroup() -> upr_vdem

### Regime Dyads ---------------------------------------------------------------
upr_vdem |> 
  group_by(to_cow, from_cow, year) |> 
  mutate(
    regime_dyads_brm = case_when(
    Target_brm_regime == "Democracy" & 
      Sender_brm_regime == "Democracy" ~ "Democracy Dyads",
    Target_brm_regime == "Autocracy" & 
      Sender_brm_regime == "Autocracy" ~ "Autocracy Dyads",
    Target_brm_regime == "Democracy" & 
      Sender_brm_regime == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_brm_regime == "Autocracy" & 
      Sender_brm_regime == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_brm = 
    factor(regime_dyads_brm, 
           levels = c("Democracy Dyads", "Autocracy Dyads",
                      "Demo(REV)-Auto(SUR) Mixed Dyads",
                      "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads_lexi = case_when(
    Target_Lexi_regime == "Democracy" & 
      Sender_Lexi_regime == "Democracy" ~ "Democracy Dyads",
    Target_Lexi_regime == "Autocracy" & 
      Sender_Lexi_regime == "Autocracy" ~ "Autocracy Dyads",
    Target_Lexi_regime == "Democracy" & 
      Sender_Lexi_regime == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_Lexi_regime == "Autocracy" & 
      Sender_Lexi_regime == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_lexi = 
    factor(regime_dyads_lexi, 
           levels = c("Democracy Dyads", "Autocracy Dyads",
                      "Demo(REV)-Auto(SUR) Mixed Dyads", 
                      "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads_lexi5 = case_when(
    Target_Lexi_regime5 == "Democracy" & 
      Sender_Lexi_regime5 == "Democracy" ~ "Democracy Dyads",
    Target_Lexi_regime5 == "Autocracy" & 
      Sender_Lexi_regime5 == "Autocracy" ~ "Autocracy Dyads",
    Target_Lexi_regime5 == "Democracy" & 
      Sender_Lexi_regime5 == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_Lexi_regime5 == "Autocracy" & 
      Sender_Lexi_regime5 == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_lexi5 = 
    factor(regime_dyads_lexi5, 
           levels = c("Democracy Dyads", "Autocracy Dyads",
                      "Demo(REV)-Auto(SUR) Mixed Dyads", 
                      "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads_lexi4 = case_when(
    Target_Lexi_regime4 == "Democracy" & 
      Sender_Lexi_regime4 == "Democracy" ~ "Democracy Dyads",
    Target_Lexi_regime4 == "Autocracy" & 
      Sender_Lexi_regime4 == "Autocracy" ~ "Autocracy Dyads",
    Target_Lexi_regime4 == "Democracy" & 
      Sender_Lexi_regime4 == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_Lexi_regime4 == "Autocracy" & 
      Sender_Lexi_regime4 == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_lexi4 = 
    factor(regime_dyads_lexi4, 
           levels = c("Democracy Dyads", "Autocracy Dyads",
                      "Demo(REV)-Auto(SUR) Mixed Dyads", 
                      "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads_pol4 = case_when(
    Target_Pol4_regime == "Democracy" & 
      Sender_Pol4_regime == "Democracy" ~ "Democracy Dyads",
    Target_Pol4_regime == "Autocracy" & 
      Sender_Pol4_regime == "Autocracy" ~ "Autocracy Dyads",
    Target_Pol4_regime == "Democracy" & 
      Sender_Pol4_regime == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_Pol4_regime == "Autocracy" & 
      Sender_Pol4_regime == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_pol4 = 
    factor(regime_dyads_pol4, 
           levels = c("Democracy Dyads", "Autocracy Dyads",
                      "Demo(REV)-Auto(SUR) Mixed Dyads", 
                      "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads = case_when(
    Target_Poly_regime == "Democracy" & 
      Sender_Poly_regime == "Democracy" ~ "Democracy Dyads",
    Target_Poly_regime == "Autocracy" & 
      Sender_Poly_regime == "Autocracy" ~ "Autocracy Dyads",
    Target_Poly_regime == "Democracy" & 
      Sender_Poly_regime == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_Poly_regime == "Autocracy" & 
      Sender_Poly_regime == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads = factor(
    regime_dyads, levels = c("Democracy Dyads", "Autocracy Dyads",
                             "Demo(REV)-Auto(SUR) Mixed Dyads", 
                             "Auto(REV)-Demo(SUR) Mixed Dyads")),
  regime_dyads_vdem = case_when(
    Target_vdem_regime == "Democracy" & 
      Sender_vdem_regime == "Democracy" ~ "Democracy Dyads",
    Target_vdem_regime == "Autocracy" & 
      Sender_vdem_regime == "Autocracy" ~ "Autocracy Dyads",
    Target_vdem_regime == "Democracy" & 
      Sender_vdem_regime == "Autocracy" ~ "Auto(REV)-Demo(SUR) Mixed Dyads",
    Target_vdem_regime == "Autocracy" & 
      Sender_vdem_regime == "Democracy" ~ "Demo(REV)-Auto(SUR) Mixed Dyads",
    T ~ NA_character_),
  regime_dyads_vdem = factor(
    regime_dyads_vdem, levels = c("Democracy Dyads", "Autocracy Dyads",
                                  "Demo(REV)-Auto(SUR) Mixed Dyads", 
                                  "Auto(REV)-Demo(SUR) Mixed Dyads"))
) |> ungroup() -> upr_vdem

### Merge5: HRC Membership -----------------------------------------------------
#### Reviewed Countries' memberships -------------------------------------------
upr_vdem |> 
  left_join(
    hrc_membership |> drop_na(),
    by = c("to_cow" = "ccode", 
           "year" = "year")) |>
  rename(to_membership = membership) -> upr_vdem

#### Reviewing Countries' memberships ------------------------------------------
upr_vdem |> 
  left_join(
    hrc_membership |> drop_na(),
    by = c("from_cow"= "ccode", 
           "year" = "year")) |>
  rename(from_membership = membership) -> upr_vdem

#### Joint HRC Membership ------------------------------------------------------
##### Make the membership variables into binaries ------------------------------
upr_vdem |> mutate( # code correction for dummy
  to_mem = case_when(
    is.na(upr_vdem$to_membership) ~ 0L,
    T ~ 1L),
  from_mem = case_when(
    is.na(from_membership) ~ 0L,
    T ~ 1L)) -> upr_vdem

##### Make a Joint HRC Membership variable as binary ---------------------------
upr_vdem |> 
  group_by(from_cow, to_cow, year) |> 
  mutate(
    joint_mem = if_else(to_mem == from_mem, 1L, 0L)) |> 
  ungroup() -> upr_vdem

#### IMF DOT Data ---------------------------------------------------------------
upr_vdem |> mutate(
  cycle_nu = case_when(
    year > 2007 & year < 2012 ~ 1L,
    year > 2011 & year < 2017 ~ 2L,
    year > 2016 & year < 2021 ~ 3L,
    T ~ NA_integer_)) -> upr_vdem

upr_vdem |> 
  drop_na(to_cow, from_cow, year, cycle_nu) |> 
  left_join(
    bilateral_undirec_cleaned |> 
      drop_na(countryAcode, countryBcode, year, cycle_nu, lntrade, lnmeantrade) |> 
      dplyr::select(countryAcode, countryBcode, cycle_nu, year, lagtrade1, lagtrade3, lagmeantrade) |> 
      unique(),
    by = c("to_cow" = "countryAcode", 
           "from_cow" = "countryBcode", 
           "year", "cycle_nu")
) -> upr_vdem_trade

### Farris et al. Human Rights Scores --------------------------------------------------------------

upr_vdem_trade |> 
  drop_na(to_cow, year) |> 
  left_join(
    hrs_sub |> drop_na(to_cow, year), 
    by = c("to_cow", "year")) |> 
  rename(target_hrs = hrs) -> upr_vdem_hrs

upr_vdem_hrs |> 
  drop_na(from_cow, year) |> 
  left_join(
    hrs_sub |> drop_na(to_cow, year), 
    by = c("from_cow" = "to_cow", "year")) |> 
  rename(sender_hrs = hrs) -> upr_vdem_hrs

### Combined GDP to represent bilateral trade
upr_vdem_hrs |> mutate(
  ln_combined_gdppc = log(gdppc_sender + gdppc_target),
  combined_gdppc = gdppc_sender + gdppc_target,
  share_combined = gdppc_sender/combined_gdppc,
  share_combined = if_else(is.na(share_combined),
                           gdppc_target/combined_gdppc, share_combined)) ->
  upr_vdem_hrs

### Save the results (merged) dataset as RDS and dta
saveRDS(upr_vdem_hrs |> rename(country = country.x) |> dplyr::select(-country.y), 
        "data/data for analysis/upr_vdem_hrs.RDS")

rio::export(upr_vdem_hrs |> rename(country = country.x) |> dplyr::select(-country.y), 
            "data/data for analysis/upr_vdem_hrs.dta")

